from decimal import Decimal
goods = {'F00001':{'name':'苹果','price':1.2},'F00002':{'name':'香蕉','price':5.5}}
buygoods = {}

# 输入exit可随时退出

# 查看商品信息
def see_goods():
    print("商品信息为：")
    for i,j in goods.items():
        print(f"编号：{i}")
        for k,l in j.items():
            print(f"{k}  {l}")
        print('-'*50)
    num1 = input("输入数字1查看商品信息，输入数字2进行商品购买,输入数字3查看购物车:")
    print('=' * 50)
    if int(num1) == 1:
        see_goods()
    if int(num1) == 2:
        buy_goods()
    if int(num1) == 3:
        see_buy_goods()


# 查看购买商品信息
def see_buy_goods():
    print("您购买的商品信息为:")
    if buygoods == {}:
        print("购物车为空")
    else:
        for i, j in buygoods.items():
            print( f"{i}:{j} ",end="  ")
        print("\n")
    num1 = input("输入数字1查看商品信息，输入数字2进行商品购买,输入数字3查看购物车:")
    print('=' * 50)
    if int(num1) == 1:
        see_goods()
    if int(num1) == 2:
        buy_goods()
    if int(num1) == 3:
        see_buy_goods()

# 根据商品编号购买商品，检测余额是否足够，够就扣款，不够就提醒
def buy_goods():
    while True:
        global num
        num = input("请输入您想要购买的商品的商品编号，输入数字1查看商品信息，输入数字2查看购物车,按exit退出：")

        if num == '1':
            see_goods()
        if num == '2':
            see_buy_goods()
        if num == 'exit':
            print("购物车商品数量为0")
            exit()
        if num in goods.keys():
            goods_num = int(input("输入需要购买的个数："))
            global enough_money
            enough_money = enough_money - float(goods[num]["price"])*goods_num
            if enough_money >= 0:
                print(f"您的余款为：{enough_money}")
                # global buygoods
                #buygoods.update(goods[num])
                buygoods2()
                print(buygoods)
            else:
                print("余额不足，请充值........")
        else:
            print("商品不存在，请重新输入：")
        num1 = input("输入数字1查看商品信息，输入数字2进行商品购买,输入数字3查看购物车:")
        print('=' * 50)
        if int(num1) == 1:
            see_goods()
        if int(num1) == 2:
            buy_goods()
        if int(num1) == 3:
            see_buy_goods()

def buygoods2():
    buygoods.update(goods[num])



print("三乐购物系统欢迎您！".center(50,"="))
user_name = input("请输入用户名,按exit退出：")
if user_name == 'exit':
    print("购物车商品数量为0")
    exit()
keyward = input("请输入用户密码，按exit退出：")
if keyward == 'exit':
    print("购物车商品数量为0")
    exit()
if user_name == "root" and keyward == "123456":
    enough_money = float(input("请输入您的预算信息，按exit退出："))

    if keyward == 'exit':
        print("购物车商品数量为0")
        exit()
    num1 = input("输入数字1查看商品信息，输入数字2进行商品购买,输入3查看购物车:")
    print('=' * 50)
    if int(num1) == 1:
        see_goods()
    if int(num1) == 2:
        buy_goods()
    if int(num1) == 3:
        see_buy_goods()
else:
    print("信息输入无效，请重新输入！")

